﻿using Ecommerce.Domain;
using Ecommerce.Domain.Eunm;
using Ecommerce.Repository.Impl.Use;
using Ecommerce.Repository.Interface.Eunms;
using Ecommerce.Repository.Interface.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ecommerce.Services.Eunm
{
    public class EunmServices : IEunmServices
    {
        private IEnumRepository enumRepository;

        private IRoleRepository RoleRepository;

        public EunmServices(IEnumRepository enumRepository, IRoleRepository roleRepository)
        {
            this.enumRepository = enumRepository;
            RoleRepository = roleRepository;
        }

        public async Task<object> Eunmlist()
        {
            var list = await enumRepository.GetAllAsync();
            var Plist = list.Select(w => new
            {
                w.CreateBy,
                w.CreateTime,
                w.UpdateTime,
                w.MenuName,
                w.Url,
                w.Eid,
                w.Pid,
                w.UpdateBy,
                name = list.Where(a => a.Eid == w.Pid).Select(a => new
                {
                    a.MenuName
                })
            });
            return Plist;
            //return await enumRepository.GetAllAsync();
        }

        public async Task<int> UpdateEunm(Meunm meunm)
        {
            meunm.Eid = meunm.Eid;
            meunm.Pid = meunm.Pid;
            meunm.MenuName = meunm.MenuName;
            meunm.Url = meunm.Url;
            return await enumRepository.Update(meunm);
        }
        public async Task<List<Meunm>> GetMeunms(int Eid)
        {
            return await enumRepository.GetAsync(w => w.Eid == Eid);
        }
        public  async Task<int> GetMeun(int Eid)
        {
            var plist = await enumRepository.GetAllAsync();
            Meunm meunm = new Meunm();
            var list = await enumRepository.GetAsync(w => w.Eid == Eid);    
            foreach ( var item in list)
            {
                plist = await enumRepository.GetAsync(w=>w.Eid== item.Pid);
            }
            if (plist.Count == 0)
            {
                meunm.Eid = list.FirstOrDefault().Eid;
                meunm.IsDel = true;
                meunm.IsTime = DateTime.Now;
                meunm.Pid = 0;
                meunm.MenuName = list.FirstOrDefault().MenuName;
                meunm.Url = list.FirstOrDefault().Url;
               
            }
            else
            {
                var Mlist = await enumRepository.GetAllAsync();
                meunm.Eid = Eid;
                meunm.IsDel = true;
                meunm.IsTime = DateTime.Now;
                meunm.MenuName = list.FirstOrDefault().MenuName;
                meunm.Url = list.FirstOrDefault().Url;
                foreach (var item in Mlist)
                {
                    item.Eid = item.Eid;
                    item.Pid = 0;
                    await enumRepository.Update(item);
                }
                //foreach (var item in Mlist)
                //{
                //    item.IsDel = false;
                //    item.IsTime = DateTime.Now;
                //    item.Pid = 0;
                //    return await enumRepository.Update(item);
                //}

            }
            return await enumRepository.Update(meunm);
        }

        public async Task<object> GetRoles()
        {
            var list = from a in await RoleRepository.GetAllAsync()
                       select new Eunmitem
                       {
                           value = a.Rid,
                           label = a.RidName,
                           children = null
                       };
            return list;
        }
    }
}
